package com.webchat.pgc.repository.dao.sys;

import com.webchat.pgc.repository.entity.sys.SysDataDictionaryDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Repository
public interface ISysDataDictionaryDetailDAO extends JpaRepository<SysDataDictionaryDetail, Long> {
    /**
     * 根据数据字典ID与编码计数
     *
     * @param dataDictionaryId 数据字典ID
     * @param code 编码
     * @return long
     */
    long countDistinctByDataDictionaryIdAndCode(Long dataDictionaryId,String code);
    /**
     * 根据数据字典ID与名称计数
     *
     * @param dataDictionaryId 数据字典ID
     * @param name 名称
     * @return long
     */
    long countDistinctByDataDictionaryIdAndName(Long dataDictionaryId,String name);
    /**
     * 根据数据字典ID与编码计数
     *
     * @param dataDictionaryId 数据字典ID
     * @param code 编码
     * @param id ID
     * @return long
     */
    long countDistinctByDataDictionaryIdAndCodeAndIdNot(Long dataDictionaryId,String code, Long id);
    /**
     * 根据数据字典ID与名称计数
     *
     * @param dataDictionaryId 数据字典ID
     * @param name 名称
     * @param id ID
     * @return long
     */
    long countDistinctByDataDictionaryIdAndNameAndIdNot(Long dataDictionaryId,String name, Long id);

    /**
     * 根据数据字典ID与和编码和名称模糊查询分页（支持 null 条件）
     *
     * @param dataDictionaryId 数据字典ID
     * @param code     编码
     * @param name     名称
     * @param pageable 分页参数
     * @return {@link Page }<{@link SysDataDictionaryDetail }>
     */
    @Query("SELECT c FROM SysDataDictionaryDetail c WHERE c.dataDictionaryId = :dataDictionaryId AND  (:code IS NULL OR c.code = :code) AND (:name IS NULL OR c.name LIKE %:name%)")
    Page<SysDataDictionaryDetail> findByDataDictionaryIdAndCodeAndNameLike(@Param("dataDictionaryId") Long dataDictionaryId,
                                                          @Param("code") String code,
                                                          @Param("name") String name,
                                                          Pageable pageable);

    /**
     * 删除数据由数据字典ID
     *
     * @param dataDictionaryId 数据字典 ID
     */
    @Transactional
    @Modifying
    @Query("DELETE FROM SysDataDictionaryDetail c WHERE c.dataDictionaryId = :dataDictionaryId")
    void deleteByDataDictionaryId(Long dataDictionaryId);

    /**
     * 按数据字典 ID 查找全部
     *
     * @param dataDictionaryId 数据字典 ID
     * @return {@link List }<{@link SysDataDictionaryDetail }>
     */
    List<SysDataDictionaryDetail> findAllByDataDictionaryId(Long dataDictionaryId);
}
